import { create } from "zustand";
import { ChatSession } from "@/types/chat-session";
import { ChatReply } from "@/types/chat-message";

interface ChatDetailPageState {
  state: {
    session: ChatSession;
    reply: ChatReply<any>;
    receiverTyping: boolean;
    enableMultipleSelect: boolean;
    selectedMessages: string[];
  };
  actions: {
    setCurrentChatDetailState: (session: ChatSession) => void;
    resetCurrentChatDetailState: () => void;
    setReply(reply: ChatReply<any>): void;
    setReceiverTyping: (typing: boolean) => void;
    setSelectedMessage: (messageIds: string[]) => void;
    resetSelectedMessages: () => void;
    addSelectedMessage: (messageId: string) => void;
    setEnableMultipleSelect: (enableMultipleSelect: boolean) => void;
  };
}

const ChatDetailPageStore = create<ChatDetailPageState>((set, get) => ({
  state: {
    session: null,
    reply: null,
    receiverTyping: false,
    enableMultipleSelect: false,
    selectedMessages: [],
  },
  actions: {
    setReply(reply: ChatReply<any>): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          reply,
        },
      }));
    },
    setReceiverTyping(typing: boolean): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          receiverTyping: typing,
        },
      }));
    },
    addSelectedMessage(messageId: string): void {
      const { selectedMessages } = get().state;
      if (selectedMessages.includes(messageId)) {
        set((state) => ({
          ...state,
          state: {
            ...state.state,
            selectedMessages: selectedMessages.filter(
              (item) => item !== messageId
            ),
          },
        }));
      } else {
        set((state) => ({
          ...state,
          state: {
            ...state.state,
            selectedMessages: [...selectedMessages, messageId],
          },
        }));
      }
    },
    setSelectedMessage(messageIds: string[]): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          selectedMessages: messageIds,
        },
      }));
    },

    setCurrentChatDetailState(session: ChatSession): void {
      console.log(session);
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          session,
        },
      }));
    },

    resetSelectedMessages(): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          selectedMessages: [],
        },
      }));
    },
    setEnableMultipleSelect(enableMultipleSelect: boolean): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          enableMultipleSelect: enableMultipleSelect,
        },
      }));
      if (!enableMultipleSelect) {
        set((state) => ({
          ...state,
          state: {
            ...state.state,
            selectedMessages: [],
          },
        }));
      }
    },
    resetCurrentChatDetailState(): void {
      set((state) => ({
        ...state,
        state: {
          ...state.state,
          currentSessionName: "",
          totalUnreadCount: 0,
        },
      }));
    },
  },
}));

export default ChatDetailPageStore;
